package com.big_data_studio.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.big_data_studio.dto.NoticeParm;
import com.big_data_studio.dto.TypeParm;
import com.big_data_studio.entity.*;
import com.big_data_studio.service.INoticeService;
import com.big_data_studio.service.INoticeTypeService;
import com.big_data_studio.util.general.R;
import com.big_data_studio.util.general.TokenUtil;
import com.big_data_studio.util.general.isDeleteUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private INoticeTypeService noticeTypeService;

    @PostMapping("addorupdate")
    public R addorupdate(@RequestBody NoticeParm noticeParm) {
        Notice notice = new Notice();
        NoticeType noticeType = new NoticeType();
        BeanUtils.copyProperties(noticeParm, notice);
        if (noticeParm.getId() == null) {
            notice.setUpdateName(TokenUtil.getJwtUser().getUpdateName());
            notice.setPushTime(LocalDateTime.now());
            notice.setCreateName(TokenUtil.getJwtUser().getCreateName());
            notice.setUpdateTime(LocalDateTime.now());
            noticeService.saveOrUpdate(notice);
            Integer id = notice.getId();
            noticeType.setNoticeId(id);
            noticeType.setTypeId(noticeParm.getTypeId());
            noticeTypeService.save(noticeType);
            return R.ok("成功");
        } else {
            notice.setUpdateTime(LocalDateTime.now());
            notice.setUpdateName(TokenUtil.getJwtUser().getUpdateName());
            noticeService.saveOrUpdate(notice);
            return R.ok("成功");
        }
    }

    @PostMapping("/list")
    public R page(@RequestBody TypeParm typeParm) {
        String option = "分页查询";
        List<Integer> integers = new ArrayList<>();
        Page<Notice> page1 = new Page<>(typeParm.getCurrent(), typeParm.getSize());
        LambdaQueryWrapper<NoticeType> noticeTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        noticeTypeLambdaQueryWrapper.eq(NoticeType::getTypeId, typeParm.getTypeId());
        List<NoticeType> list = noticeTypeService.list(noticeTypeLambdaQueryWrapper);
        List<Notice> notices = list.stream().map((item) -> {
            LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Notice::getId, item.getNoticeId());
            Notice one = noticeService.getOne(queryWrapper);
            queryWrapper.clear();
            return one;
        }).collect(Collectors.toList());
        QueryWrapper<Notice> noticeLambdaQueryWrapper = new QueryWrapper<>();
        for (Notice notice : notices) {
            if (notice.getIsDelete() != 1) {
                integers.add(notice.getId());
            }
        }
        if (integers.size() == 0) {
            return R.error("查询为空");
        }
        noticeLambdaQueryWrapper.in("id", integers);
        Page<Notice> page = noticeService.page(page1, noticeLambdaQueryWrapper);
        return isDeleteUtil.isdelete(0, page, option);
    }

    @GetMapping("/delete")
    public R delete(Integer id) {
        String option = "删除";
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("is_delete", 1);
        noticeService.update(updateWrapper);
        return isDeleteUtil.isdelete(0, option);
    }


    @GetMapping("/getOne")
    public R getOne(Integer id) {
        String option = "获取单个";
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getId, id);
        Notice one = noticeService.getOne(queryWrapper);
        return isDeleteUtil.isdelete(one.getIsDelete(), one, option);
    }

    @GetMapping("/batchdelete")
    public R batchdelete(Integer[] id) {
        String option = "批量删除";
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        for (Integer integer : id) {
            updateWrapper.eq("id", integer);
            updateWrapper.set("is_delete", 1);
            noticeService.update(updateWrapper);
            updateWrapper.clear();
        }
        return isDeleteUtil.isdelete(0, option);
    }
}
